#include "CardType.h"
#include "CardTypeData.h"
#include "CardTypeManager.h"
#include "PokerData.h"
#include <iostream>

TwoPair::TwoPair() {
}

TwoPair::~TwoPair() {
}

CardTypeData TwoPair::getCardType(const PokerData & data) const {
  vector<Poker> pokers = data.getPokers();
  int count = pokers.size();
  bool two = false;
  int value = 0;
  if (count > 4) {
    PokerData::sortData(pokers);
    for (int i = 0; i < count - 1; i++) {
      if (pokers.at(i).value == pokers.at(i+1).value) {
        two = true;
        value = pokers.at(i).value;
        break;
      }
    }
    if (two) {
      int secondValue = 0;
      bool one = false;
      for (int i = 0; i < count - 1; i++) {
        const Poker & p = pokers.at(i);
        if (p.value != value && p.value == pokers.at(i + 1).value) {
          one = true;
          secondValue = p.value;
          break;
        }
      }

      if (one)  {
        CardTypeData cardTypeData;
        cardTypeData.type = CardType::TwoPair;
        cardTypeData.first = value;
        cardTypeData.second = secondValue;
        for (int i = 0; i < pokers.size(); i++) {
          const Poker & p = pokers.at(i);
          if (p.value != value && p.value != secondValue) {
            cardTypeData.third = p.value;
            break;
          }
        }
        return cardTypeData;
      }
    }
  }
  return getNextType()->getCardType(data);
}

const CardType * TwoPair::getNextType() const {
  return CardTypeManager::sharedCardTypeManager()->getCardType(CardType::OnePair);
}
